﻿using Help;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BLLA
{
    public partial class Order_OrganizationCheckOut_T : BaseBLL<Model.Order_OrganizationCheckOut_T>, IBLL.IOrder_OrganizationCheckOut_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();
        IBLL.IBLLSession bl = DI.SpringHelper.GetObject<IBLL.IBLLSession>("BLLSession");
        public string CountCheckOut(int pageIndex, int pageSize, string MerID, int OrgID, string Time, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 业务流程
            int tm = int.Parse(DateTime.Now.AddDays(-1).ToString("yyyyMMdd"));
            if (tm < int.Parse(Time))
            {
                result.BS = "-1";
                result.Msg = "不允许结此日期的帐！";
                return os.StringOfObject(result, RETID);
            }
            List<Model.Order_OrganizationCheckOut_T> OrgList = new List<Model.Order_OrganizationCheckOut_T>();
            try
            {

                OrgList = base.GetPagedList<int>(1, 1, u => u.MerID == MerID & u.OrgID == OrgID, u => u.FlowOrgID);
            }
            catch (Exception)
            {
                result.BS = "-1";
                result.Msg = "查询不到数据！";
                return os.StringOfObject(result, RETID);
            }
            List<Model.Order_FlowOrganization_T> FlowList = new List<Model.Order_FlowOrganization_T>();
            if (OrgList != null && OrgList.Count != 0)
            {

                DateTime bb = DateTime.Parse(OrgList[0].OrgEndDate.Substring(0, 4) + "-" + OrgList[0].OrgEndDate.Substring(4, 2) + "-" + OrgList[0].OrgEndDate.Substring(6, 2) + " 0:0:0");
                string str_开始对帐时间 = bb.AddDays(+1).ToString("yyyyMMdd");
                FlowList = base.DBSession.IOrder_FlowOrganization_TDAL.GetListBy("SELECT  *  FROM Order_FlowOrganization_T where MerID='" + MerID + "'OrgFlowType='1A0002' and OrgFlowTime>" + str_开始对帐时间 + "000000 and OrgFlowTime <" + Time + "235959 and OrgID=" + OrgID);
            }
            else
            {
                FlowList = base.DBSession.IOrder_FlowOrganization_TDAL.GetListBy("SELECT * FROM Order_FlowOrganization_T where  MerID='" + MerID + "' and OrgFlowType='1A0002' and OrgFlowTime <" + Time + "235959  and OrgID=" + OrgID);

            }
            double d_应收 = 0;
            foreach (var item in FlowList)
            {
                d_应收 += item.OrgRepayAmount;
            }
            CountCheck cc = new CountCheck();
            cc.FlowList = FlowList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            cc.TotalFlowNum = d_应收;
            result.BS = "1";
            result.Msg = "查询成功！";
            result.rows = cc;
            result.total = FlowList.Count;
            return os.StringOfObject(result, RETID);
            #endregion
        }

        public string CheckOut(string MerID, int OrgID, string Time, string Paid, int OperID, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 业务流程
            int tm = int.Parse(DateTime.Now.AddDays(-1).ToString("yyyyMMdd"));
            if (tm < int.Parse(Time))
            {
                result.BS = "-1";
                result.Msg = "不允许结此日期的帐！";
                return os.StringOfObject(result, RETID);
            }
            List<Model.Order_OrganizationCheckOut_T> OrgList = new List<Model.Order_OrganizationCheckOut_T>();
            try
            {

                OrgList = base.GetPagedList<int>(1, 1, u => u.MerID == MerID & u.OrgID == OrgID, u => u.FlowOrgID);
            }
            catch (Exception)
            {
                result.BS = "-1";
                result.Msg = "查询不到数据！";
                return os.StringOfObject(result, RETID);
            }
            List<Model.Order_FlowOrganization_T> FlowList = new List<Model.Order_FlowOrganization_T>();
            string str_开始对帐时间 = "";
            if (OrgList != null && OrgList.Count != 0)
            {

                DateTime bb = DateTime.Parse(OrgList[0].OrgEndDate.Substring(0, 4) + "-" + OrgList[0].OrgEndDate.Substring(4, 2) + "-" + OrgList[0].OrgEndDate.Substring(6, 2) + " 0:0:0");
                str_开始对帐时间 = bb.AddDays(+1).ToString("yyyyMMdd");
                FlowList = base.DBSession.IOrder_FlowOrganization_TDAL.GetListBy("SELECT  *  FROM Order_FlowOrganization_T where OrgFlowType='1A0002' and OrgFlowTime>" + str_开始对帐时间 + "000000 and OrgFlowTime <" + Time + "235959");
            }
            else
            {

                FlowList = base.DBSession.IOrder_FlowOrganization_TDAL.GetListBy("SELECT * FROM Order_FlowOrganization_T where  OrgFlowType='1A0002' and OrgFlowTime <" + Time + "235959");
                str_开始对帐时间 = FlowList[0].OrgFlowTime.Substring(0, 8);
            }
            int d_应收 = 0;
            foreach (var item in FlowList)
            {
                d_应收 += item.OrgRepayAmount;
            }
            d_应收 = Math.Abs(d_应收);
            Model.Order_OrganizationCheckOut_T OCO = new Model.Order_OrganizationCheckOut_T();
            OCO.FlowOrgID = int.Parse(GetSysKey.GetKey("OrgCOKey"));
            OCO.MerID = MerID;
            OCO.OrgID = OrgID;
            OCO.OrgStartDate = str_开始对帐时间;
            OCO.OrgEndDate = Time;
            OCO.OrgAmount = d_应收;
            OCO.OrgRepayAmount = int.Parse(Paid);
            OCO.OrgFlowTime = DateTime.Now.ToString("yyyyMMddHHmmss");
            OCO.OrgFlowOper = OperID;
            Model.Order_Organization_T OT = base.DBSession.IOrder_Organization_TDAL.GetListBy(u => u.OrgID == OrgID).First();
            if (OT.OrgWaltBalance + d_应收 > OT.OrgWaltLimit)
            {
                OT.OrgWaltBalance = OT.OrgWaltLimit;
            }
            else
            {
                OT.OrgWaltBalance += d_应收;
            }
            if (base.DBSession.IOrder_Organization_TDAL.Modify(OT) == 0)
            {
                result.BS = "-3";
                result.Msg = "更新钱包失败！";
                result.rows = d_应收;
                return os.StringOfObject(result, RETID);
            }
            OCO.OrgAfterLimit = OT.OrgWaltBalance;
            if (base.Add(OCO) > 0)
            {
                result.BS = "1";
                result.Msg = "结帐成功！";
            }
            else
            {
                result.BS = "";
                result.Msg = "结帐失败！";
            }
            return os.StringOfObject(result, RETID);
            #endregion
        }

       
 
    }
}
